package com.ytc.restcontroller;

import com.ytc.dto.BookFeiLeiDto;
import com.ytc.mapper.BooklistMapper;
import com.ytc.mapper.CommentMapper;
import com.ytc.mapper.FileInfoMapper;
import com.ytc.mapper.KeywordMapper;
import com.ytc.model.Book;
import com.ytc.model.Booklist;
import com.ytc.model.FileInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class BookListServiceImpl {
    @Autowired
    private BooklistMapper booklistMapper;
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private FileInfoMapper fileInfoMapper;

    @Autowired
    private KeywordMapper keywordMapper;

    //zzm 新增作品
    @RequestMapping("addBookList")
    public void addBookList(@RequestBody Booklist booklist, @RequestParam("kId") String kId){
        //System.out.println("impl" + booklist);
        //先新增文件
        FileInfo f = booklist.getFileInfo();
        fileInfoMapper.addFile(f);
        booklist.setFileInfo(f);
        booklistMapper.addBookList(booklist);
        //新增关键词与作品的关联关系
        List<Integer> list = new ArrayList<>();
        String[] split = kId.split(",");
        for (String kid:split){
            list.add(Integer.parseInt(kid));
        }
        keywordMapper.addBookKeyword(booklist.getBlId(),list);
    }

    //柴晓颖：查询书籍信息去修改
    @RequestMapping("queryBookListByBookid")
    public Booklist queryBookListByBookid(@RequestParam("bookid") Integer bookid){
        return booklistMapper.queryBookListByBookid(bookid);
    }

    //柴晓颖：删除书籍
    @RequestMapping("delBook")
    public void delBook(@RequestParam("bookid") Integer bookid){
        booklistMapper.deleteByPrimaryKey((long)bookid);
    }

    //柴晓颖：通过作者查对应的书籍
    @RequestMapping("queryBookListByAuthor")
    public List<Booklist> queryBookListByAuthor(@RequestParam("uId") Integer uId){
        return booklistMapper.queryBookListByAuthor(uId);
    }

    //柴晓颖：作者申请签约，修改状态
    @RequestMapping("applyQianyue")
    public void applyQianyue(@RequestParam("blId") Integer blId){
        booklistMapper.applyQianyue(blId);
    }

    //zzm:查询作者书籍总数
    @RequestMapping("queryScountByUid")
    public long queryScountByUid(@RequestParam("uId") Integer uId){
        return booklistMapper.queryScountByUid(uId);
    }

    /**
     * 张阔：分类条件查询
     * @param dto
     * @return
     */
    @RequestMapping("queryBookListByDto")
    public List<Booklist> queryBookListByDto(@RequestBody BookFeiLeiDto dto){
        return booklistMapper.queryBookListByDto(dto);
    }

    /**
     * 张阔：查询书籍信息
     * @param blId
     * @return
     */
    @RequestMapping("queryBookById")
    public Booklist queryBookById(@RequestParam("blId") Integer blId){
        return booklistMapper.queryBookById(blId);
    }

    //张阔：查平均分
    @RequestMapping("queryAvgScore")
    public Double queryAvgScore(@RequestParam("blId") Integer blId){
        return commentMapper.queryAvgScore(blId);
    }

    //张阔：修改点击量
    @RequestMapping("updateBookCount")
    public void updateBookCount(@RequestParam("bid")  Integer bid,@RequestParam("cid")  Integer cid){
        booklistMapper.updateBookCount(bid,cid);
    }


}
